<?php
/*

Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

$semf_root = "../../";

$action=$_GET['action'];
$project_dir = $_GET['project_dir'];


function deldir($dir) {
	$dh=opendir($dir);
	while ($file=readdir($dh)) {
		if($file!="." && $file!="..") {
			$fullpath=$dir."/".$file;
			if(!is_dir($fullpath)) {
			unlink($fullpath);
			} else {
			deldir($fullpath);
			}
		}
	}
	closedir($dh);
	
	if(rmdir($dir)) {
		return true;
	} else {
		return false;
	}
}
//for a new file 
if($action == 'new')
{
 	//new filename..
	$module_name = $_GET['module_name'];
	$dir = $semf_root.$project_dir.$module_name;
	if(!is_dir($dir))
	{
			mkdir($dir);
			$module_xml = $dir.'/module.xml';
	
		$doc = new DOMDocument();
		$root = NULL;
		
		//Assign element module to root
		if (!file_exists($module_xml)) {
			$root = $doc->createElement('module');
			$doc-> appendChild($root);
		}
		$doc->save($module_xml);

		echo "successful";
	}
}
else if($action == 'rename' )
{
	$new_name = $_GET['new_name'];
	
 	//to be filled..
}
else if( $action == 'delete')
{
	$module_name = $_GET['module_name'];
 	$dir = $semf_root.$project_dir.$module_name;
	
	if(is_dir($dir))
	{
		deldir($dir);
	}
}
else if($action == 'up')
{
	$module_name = $_GET['module_name'];
	$dir = $semf_root.$project_dir.$module_name."/";
	
	$target_dir = $semf_root.$_GET['target']."/";
	
	copy_dir($dir, $target_dir, $module_name);
	
}
else if($action == 'down')
{
	$dir = $semf_root.$project_dir."/";
	$source_dir = $semf_root.$_GET['source']."/";
	$module_name = basename($semf_root.$_GET['source']);
	
	$target_dir = $dir.$module_name;
	$i = 0;
	while(is_dir($target_dir) || is_file($target_dir))
	{
		$i ++;
		$target_dir = $dir.$module_name.$i;
	}
	$module_name .= $i;
	
	$module_xml = $semf_root.$_GET['source']."/module.xml";
	resolve_dependencies($module_xml, $module_name, $semf_root.$project_dir);
	
	copy_dir($source_dir, $dir, $module_name);
}

function copy_dir($src, $dest, $justDest)
{
	if( !is_dir($src) ) //then just try copying it
	{
		if (!copy($src, $dest.$justDest))
		{
			return -1;
		}
		return 0;
	}
	if($justDest != NULL)
	{
		//sure src is a directory..
		$dest .= $justDest."/";
		if(!is_dir($dest))
		{
			mkdir($dest);
		}
	}
	
	$d = dir($src);
	while (false !== ($file = $d->read()))
	{
		if (($file{0} == '.') ||
			($file{0} == '..') ||
			($file{0} == '~'))
		{
			continue;
		}
		else if( is_dir($src.$file) )
		{
			if( !is_dir($dest.$file) ) //just check before creatng..
			{
				//echo $dest.":".$dest.$file."\n";
				mkdir($dest.$file);
			}
			$ret = copy_dir($src.$file."/", $dest.$file."/", NULL);
			if( $ret == -1 )
			{
				return -1;
			}
			continue;
		}
		//then it sould be a file..
		if (!copy($src.$file, $dest.$file))
		{
			return -1;
		}
	}
	return 0;	
}

function resolve_dependencies($module_xml, $module_name, $project_home)
{
	$doc = new DOMDocument();
	$doc->load($module_xml);
	
	$table_elements = $doc->documentElement->childNodes;
	
	foreach ($table_elements as $table_element)
	{
		if($table_element->nodeType != XML_ELEMENT_NODE)
		{
			continue;
		}
		$table_name = $table_element->getAttribute("name");
		
		//load all the inherited information..
		$inherited_elements = $table_element->childNodes;
		foreach($inherited_elements as $inherited_element)
		{
			if($inherited_element->tagName != "inherited")
			{
				continue;
			}
			
			$inherited_table = $inherited_element->nodeValue;
			$pos = strpos($inherited_table, ".");
			$inherited_table_owner_module = substr($inherited_table, 0, $pos);
			$inherited_table_name = trim(substr($inherited_table, $pos+1));
			
			
			$inherited_module_xml = $project_home.trim($inherited_table_owner_module)."/module.xml";
			$inherited_doc = new DOMDocument();
			$inherited_doc->load($inherited_module_xml);
			
			$inherited_table_elements = $inherited_doc->documentElement->childNodes;
			
			$current_table_full_name = $module_name.".".$table_name;
			
			$found_in_parent = false;
			foreach ($inherited_table_elements as $inherited_table_element)
			{
				
				if($inherited_table_element->nodeType != XML_ELEMENT_NODE)
				{
					continue;
				}
				if($inherited_table_element->getAttribute("name") == $inherited_table_name)
				{
					$inherited_field_elements = $inherited_table_element->childNodes;
					/*$inherited_field_elements = 
						array_merge($inherited_field_elements, $field_elements);*/
					foreach($inherited_field_elements as $inherited_field_element)
					{			
						if($inherited_field_element->tagName != "child")
						{
							continue;
						}
						$child_name = trim($inherited_field_element->firstChild->nodeValue);
						
						if($child_name == $current_table_full_name)
						{
							$found_in_parent = true;
							break;
						}
					}
					if($found_in_parent == false)
					{
						//should add it to the parent..
						$new_child_element = $inherited_doc->createElement("child");
						$inherited_table_element->appendChild($new_child_element);
						
						$child_text = 
							$inherited_doc->createTextNode($current_table_full_name);
							
						$new_child_element->appendChild($child_text);
						
						$inherited_doc->save($inherited_module_xml);
					}
					break;
				}
			}
		}
	}
}
?>
